<?php
include'Utils.php';

class UsersController extends AppController{
	
	// Declare used components
	public $components = array('Subject', 'CenterInfo', 'Pic', 'MainInfo');
	
	/**
	 * Admin function used to perform change password task
	 */ 
	function change_password($inform = NULL){
		
		// Check session and redirect to login page
		Utils::checkSession($this);
		$this->set('admin', '1');
		
		// Get all subjects
		$subjects = $this->Subject->getListSubject();
		$this->set('subjects', $subjects);
		// Get center information
		$centerInfo = $this->CenterInfo->getInfo();
		$this->set('center_info', $centerInfo);
		
		/* Get User from DB */
		$users = $this->User->find('all');
		$this->set('users', $users);
		$this->set('inform', $inform);
		if ($this->request->is('post')){
			
			if (strcmp($_POST["oldPass"], $users[0]['User']['m_user_password']) != 0){
				$this->redirect((array('action' => 'change_password', "Fail")));
			}
			
			$dataNeedToSave = array('User.m_user_password' => $_POST["newPass"]);
			$saveCondition = array('User.m_user_id' => $users[0]['User']['m_user_id']);
			
			if ($this->User->updateAll($dataNeedToSave, $saveCondition)) {
				$this->redirect((array('action' => 'change_password', "Success")));
			}
		}
	}
	
	/**
	 * Admin function used to display change password success page
	 */ 
	function change_password_success(){
		
		// Check session and redirect to login page
		Utils::checkSession($this);
		$this->set('admin', '1');
		
		// Get common information
		$centerInfo = $this->CenterInfo->getInfo();
		$this->set('center_info', $centerInfo);
	}
	
	/**
	 * Admin function used to display and process login page
	 */ 
	function login(){
		session_start();
		if(isset($_SESSION['user_id'])){
			$this->redirect(array('controller'=>'Users',
					'action'=>'userHome'));
		}else{
			$this->set('admin', '1');
			// Get common information
			$centerInfo = $this->CenterInfo->getInfo();
			$this->set('center_info', $centerInfo);
			$isLogin = isset($_POST['isLogin']) ? $_POST['isLogin'] : '';
			if($isLogin != ''){
				
				$password = $_POST['pass'];
				$count = $this->User->find('count', array(
					'conditions' => array('User.m_user_password' => $password)
				));
				if($count > 0){
					$users = $this->User->find('all', array(
						'conditions' => array('User.m_user_password' => $password)
					));
					$_SESSION['user_id'] = $users[0]['User']['m_user_id'];
					
					$this->redirect(array('controller'=>'Users',
						'action'=>'userHome'));
				}else{
					$this->set("msg", "(Mật khẩu không đúng.)");
				}
			}
		}
	}
	
	/**
	 * Function used to display admin homepage
	 */
	function userHome(){
		// Check session is existed???
		Utils::checkSession($this);
		$this->set('admin', '1');
		// Get common information
		$centerInfo = $this->CenterInfo->getInfo();
		$this->set('center_info', $centerInfo);

	}
	
	/**
	 * Function used to process logout and display login page
	 */
	function logout(){
		
		// Check session and redirect to login page
		Utils::checkSession($this);
		$this->set('admin', '1');
		
		unset($_SESSION["user_id"]);
		session_destroy();
		$this->redirect(array('controller'=>'Pages', 'action' => 'display'));
	}
}
?>